<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>资源列表</title>
    <link rel="stylesheet" type="text/css" href="../../css/aui2/aui.css"/>
    <style>
    	body {
	        min-height: 100%;
	    }
	    .item { padding-left: 0 !important;}
        .aui-searchbar {
            background: transparent;
        }
        .aui-bar-nav .aui-searchbar-input {
            background-color: #ffffff;
        }
        .aui-bar-light .aui-searchbar-input {
            background-color: #f5f5f5;
        }
        .aui-info span{
            display:inline-block;
        }
        .smaller{
            font-size:0.5rem;
        }
        .aui-info{
            padding:0;
        }
    </style>
</head>
<body>
    <div class="aui-tab" id="tab">
        <div class="aui-tab-item aui-active">微课</div>
        <div class="aui-tab-item"><div></div>直播</div>
        <div class="aui-tab-item">试卷</div>
        <div class="aui-tab-item">讨论</div> 
    </div>
    <div class="aui-card-list-content-padded aui-border-b aui-border-t">
        	<script id="resource_temp" type="text/x-dot-template">
        	   {{if(it.index==2) { }}
                    {{ for(var i=0; i< it.data.length; i++) { }}
                        <div class="aui-col-xs-6">
                            <div onclick="openCourse({{=count+i}})">
                                <img src={{=it.path+it.data[i].cover}} onerror="this.src='../../image/main/c1.jpg'" />
                                <div class="aui-info" style="padding-top:0">
                                    <span>{{=it.data[i].title}}</span><span>{{=it.data[i].teacher}}</span>
                                </div>
                                <div class="aui-info smaller" style="padding-top:0">
                                    <span>{{=it.data[i].start_time?it.data[i].start_time.substring(0,16):""}}</span><span>{{=it.data[i].status==0?"即将直播":"正在直播"}}</span>
                                </div>
                            </div>
                        </div>
                    {{ } }}
               {{ }else { }}
        			{{ for(var i=0; i< it.data.length; i++) { }}
        	            <div class="aui-col-xs-6">
        		            <div onclick="openCourse({{=count+i}})">
        		                <img src={{=it.path+it.data[i].cover}} onerror="this.src='../../image/main/c1.jpg'" />
        		                <div class="aui-info" style="padding-top:0">
        		                    <span>{{=it.data[i].title}}</span>
        		                </div>
        		                <div class="aui-info smaller" style="padding-top:0">
                                    <span>{{="教师："+it.data[i].teacher}}</span><span>{{=it.data[i].start_time?it.data[i].start_time.substring(0,10):""}}</span>
                                </div>
        		            </div>
        		        </div>
        		    {{ } }}
        	    {{ } }}
    		</script>
        <div class="aui-row aui-row-padded" id="resource_list">
        </div>
    </div>
</body>
<script src="../../script/doT.min.js"></script>
<script data-main="../../script/main" src="../../script/require.js"></script>
<script type="text/javascript">
	var count = 0;
	var index = 1;
	var isbottom = false;//是否到底
    var isRefresh = true;//是否刷新
	var token;
	var temp_data = [];
	apiready = function(){
		require(["zepto","common","aui2/aui-tab"],function(){
			var tab = new auiTab({
                element:document.getElementById("tab"),
		    },function(ret){
		    	index = ret.index;
		    	count = 0;
		    	isbottom = false;
		    	isRefresh = true;
		    	sendAjax();
		    });
		    $zb.prefs.getPrefs('token',function(ret){
		    	token = ret.value;
		    	sendAjax();
		    })
		    
		    api.setRefreshHeaderInfo({
                loadingImg: 'widget://image/refresh.png',
                bgColor: '#ccc',
                textColor: '#fff',
                textDown: '下拉刷新...',
                textUp: '松开刷新...'
            }, function(ret, err) {
                api.refreshHeaderLoadDone();
                if(ret){
                    isbottom = false;
                    isRefresh=true;
                    sendAjax();
                }
            });
            
            api.addEventListener({
                name:'scrolltobottom',
                extra:{
                    threshold:0            //设置距离底部多少距离时触发，默认值为0，数字类型
                }
            }, function(){
                isRefresh = false;
                isbottom?api.toast({
                    msg:'没有更多了'
                }):sendAjax();        
            });
		})
	};
	
	function sendAjax(filter){
		var data = {
			values:{
				token:token,
				type : index,
				name:filter,
				appindex:count
			}
		}
		$zb.requestForData(data,'resources','getMyResources',function(ret){
		    if(ret&&ret.status==="OK"){
                var temp = {
                    data:ret.data,
                    index:index,
                    path:$zb.getRes()
                }
                if(isRefresh){
                    temp_data = ret.data;
                    $("#resource_list").html(doT.template($("#resource_temp").text())(temp));
                }else{
                    temp_data = temp_data.concat(ret.data);
                    $("#resource_list").append(doT.template($("#resource_temp").text())(temp));
                }
                if(ret.data.length===10){
                    count+=10;
                }else{
                    isbottom = true;
                }
		    }
		});
	}
	
	function openCourse(c_index){
	   var data = {
	       values:{
	           token:token,
	           id:temp_data[c_index].id
	       }
	   }
	   $zb.requestForData(data,'resources','getResourceDetail',function(ret){
            if(ret&&ret.status==="OK"){
        		api.openWin({
                    name: 'rs_detail_win',
                    url: '../resource/rs_detail_win.html',
                    pageParam:{
                    	data:ret.data,
                    	index:index,
                    	info:data.values,
                    	img:$zb.getRes()+temp_data[c_index].cover
                    },
                    slidBackEnabled:false
                });
            }
	   });
	}
	
</script>
</html>